package com.shujia.data

import java.util.Properties

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

import scala.io.Source

object CarDataToKafka {
  def main(args: Array[String]): Unit = {


    //创建kafka的生产者
    val properties = new Properties

    //1、指定kafkabroker地址
    properties.setProperty("bootstrap.servers", "master:9092")

    //2、指定kv的序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")

    val producer = new KafkaProducer[String, String](properties)



    //读取过车数据
    Source
      .fromFile("C:\\Users\\qx\\Documents\\WPS Cloud Files\\22672127\\团队文档\\十二期\\项目\\智慧交通\\cars.json")
      .getLines()
      .foreach(car => {

        //将数据写入kafka
        val record = new ProducerRecord[String, String]("car", car)

        producer.send(record)
        producer.flush()

        //停一会
        Thread.sleep(20)
      })


  }
}
